package com.cim.web;

import com.cim.config.GlobalConfig;
import com.cim.pojo.Account;
import com.cim.pojo.Course;
import com.cim.pojo.CourseAccount;
import com.cim.pojo.Video;
import com.cim.service.AccountService;
import com.cim.service.CourseAccountService;
import com.cim.service.CourseService;
import com.cim.service.VideoService;
import com.cim.utils.PageUtil;
import com.cim.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Api(tags="选课相关")
@RequestMapping("/api/ca")
@RestController
public class CAController {

    @Autowired
    CourseAccountService courseAccountService;
    @Autowired
    AccountService accountService;
    @Autowired
    CourseService courseService;
    @Autowired
    VideoService videoService;
    @ApiOperation(value="课程关联判断",notes="判断传入课程是否是当前用户的课程")
    @PostMapping("isMyCourse")
    public Object isMyCourse(@RequestParam @ApiParam("课程Id") String courseId, HttpServletRequest request) {
        String userName= (String) request.getAttribute(GlobalConfig.TOKEN_USER);
        Account account=accountService.getAccountByUserName(userName);
        CourseAccount courseAccount=courseAccountService.findByCA(account.getUserId(),courseId);
        if(courseAccount!=null){
            return ResponseUtil.ok(courseAccount);
        }else{
            return ResponseUtil.ok(false);
        }
    }

    //获取选课信息
    @ApiOperation(value="获取当前用户选课信息",notes="使用token获取当前用户选课信息")
    @PostMapping("/getOpt")
    public Object getca(HttpServletRequest request,
                        @RequestParam(defaultValue= "1") @ApiParam(value="页码") Integer index,
                        @RequestParam(defaultValue= "10") @ApiParam(value="页面大小") Integer pageSize,
                        @RequestParam(defaultValue="false") @ApiParam("是否直播课程") String live ){
        String username=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        if(username==null||username.equals("")){
            return ResponseUtil.fail(404,"找不到数据");
        }
        Account ac=accountService.getAccountByUserName(username);
        if(ac==null){
            return ResponseUtil.fail(404,"找不到数据");
        }
        CourseAccount courseAccount=new CourseAccount();
        courseAccount.setUserId(ac.getUserId());
        List<CourseAccount> courseAccountList=courseAccountService.findCourseAccount(courseAccount);
        List<Course> courseList=new ArrayList<Course>();
        for(CourseAccount ca:courseAccountList){
            Course course=courseService.getCourse(ca.getCourseId());
            if(course.getLiveCourse().equals(live)){
                courseList.add(course);
            }
        }
        PageUtil<Course> result=new PageUtil<Course>(courseList);
        result.setIndex(index);
        result.setPageSize(pageSize);
        return ResponseUtil.okList(result);
    }
    
    @ApiOperation("选课")
    @PostMapping("/select")
    public Object selectCA(@RequestParam @ApiParam("课程Id") String courseId,
                           HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        Account account=accountService.getAccountByUserName(user);
        if(account==null){
            return ResponseUtil.unlogin();
        }
        Course course=courseService.getCourse(courseId);
        if(course==null){
            return ResponseUtil.fail(404,"找不到课程数据");
        }
        CourseAccount ca=new CourseAccount();
        ca.setCourseId(course.getCourseId());
        ca.setUserId(account.getUserId());
        courseAccountService.addCourseAccount(ca);
        return ResponseUtil.ok(ca);
    }

    @ApiOperation("存储课程进度")
    @PostMapping("/saveInfo")
    public Object saveInfo(@RequestParam @ApiParam("课程Id") String courseId,
                           @RequestParam @ApiParam("视频Id") String videoId,
                           @RequestParam @ApiParam("视频进度") Integer videoProgress,
                           HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        Account account=accountService.getAccountByUserName(user);
        if(account==null){
            return ResponseUtil.unlogin();
        }
        Course course=courseService.getCourse(courseId);
        if(course==null){
            return ResponseUtil.fail(404,"找不到课程数据");
        }
        Video video=videoService.getVideo(videoId);
        if(video==null){
            return ResponseUtil.fail(404,"找不到视频数据");
        }
        CourseAccount courseAccount=courseAccountService.findByCA(account.getUserId(),courseId);
        if(courseAccount==null){
            return ResponseUtil.unauthz();
        }
        if(!course.getCourseId().equals(video.getCourseId())){
            return ResponseUtil.fail(412,"请求的课程与视频无关联");
        }
        courseAccount.setVideoId(videoId);
        courseAccount.setVideoProgress(videoProgress);
        courseAccountService.updateCourseAccount(courseAccount);
        return ResponseUtil.ok(courseAccount);
    }

    @ApiOperation("退选课程")
    @PostMapping("/unselect")
    public Object unSelect(@RequestParam @ApiParam("课程Id") String courseId,
                           HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        Account account=accountService.getAccountByUserName(user);
        if(account==null){
            return ResponseUtil.unlogin();
        }
        Course course=courseService.getCourse(courseId);
        if(course==null){
            return ResponseUtil.fail(404,"找不到课程数据");
        }
        CourseAccount courseAccount=courseAccountService.findByCA(account.getUserId(),course.getCourseId());
        if(courseAccount==null){
            return ResponseUtil.ok();
        }
        courseAccountService.deleteCourseAccount(courseAccount.getCourseAccountId());
        return ResponseUtil.ok();
    }
}
